﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PrdTaskInfoForm.aspx.cs"
    Inherits="FX.CC.Andon.WebSite.BasicDataManage.PrdTaskInfoForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../lib/boot.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" method="post">
    <input name="TaskID" class="mini-hidden" />
    <fieldset style="border: solid 1px #aaa; padding: 5px;">
        <legend>基本信息</legend>
        <div style="padding-left: 11px; padding-bottom: 5px;">
            <table style="table-layout: fixed;">
                <tr>
                    <td style="width: 100px;" align="right">
                        产线：
                    </td>
                    <td style="width: 250px;" align="left">
                        <div id="LineID" name="LineID" class="mini-combobox" style="width: 250px;" popupwidth="300"
                            required="true" textfield="LineName" valuefield="LineID" url="../Service/BasicDataManageHandler.ashx?action=GetLineListByCombobox">
                            <div property="columns">
                                <div header="产线编号" field="LineCode">
                                </div>
                                <div header="产线名称" field="LineName">
                                </div>
                            </div>
                        </div>
                    </td>
                    <td style="width: 100px;" align="right">
                        日期：
                    </td>
                    <td style="width: 250px;" align="left">
                        <input name="BizDate" vtype="date:yyyy-MM-dd" style="width: 250px;" required="true"
                            class="mini-datepicker" />
                    </td>
                </tr>
                <tr>
                    <td style="width: 100px;" align="right">
                        班次：
                    </td>
                    <td style="width: 250px;" align="left">
                        <input name="ShiftID" class="mini-combobox" style="width: 250px;" textfield="ShiftName"
                            valuefield="ShiftID" url="../Service/BasicDataManageHandler.ashx?action=GetShiftListByCombobox"
                            required="true" />
                    </td>
                </tr>
            </table>
        </div>
    </fieldset>
    <fieldset style="border: solid 1px #aaa; padding: 5px; height: 300px;">
        <legend>生产计划</legend>
        <div class="mini-toolbar" style="padding: 2px; border-bottom: 0;">
            <table style="width: 100%;">
                <tr>
                    <td style="width: 100%;">
                        <a class="mini-button" iconcls="icon-add" onclick="addRow();" plain="true">添加</a>
                        <a class="mini-button" iconcls="icon-remove" onclick="removeRow()" plain="true">删除</a>
                    </td>
                </tr>
            </table>
        </div>
        <!--撑满页面-->
        <div class="mini-fit">
            <div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 100%;" allowcelledit="true"
                allowcellselect="true" showfooter="false" oncellcommitedit="OnCellCommitEdit"
                oncellbeginedit="OnCellBeginEdit">
                <div property="columns">
                    <div type="checkcolumn">
                    </div>
                    <div field="PrdID" displayfield="PrdName" width="120" headeralign="center" allowsort="false">
                        产品
                        <%--<input property="editor" class="mini-combobox" style="width: 100%;" valuefield="PrdID"
                            textfield="PrdName" url="../Service/BasicDataManageHandler.ashx?action=GetProductListByCombobox" />--%>

                            <input id="PrdID"  property="editor" class="mini-autocomplete" required="true" 
                            style="width:100%;" popupwidth="100%" required="true" popupheight="150"
                            valueField="PrdID" textField="PrdCode"
                            url="../Service/BasicDataManageHandler.ashx?action=GetProductListByQuery" />
                    </div>
                    <div field="EqID" displayfield="EqName" width="140" headeralign="center" allowsort="false">
                        设备
                        <input property="editor" class="mini-buttonedit" onbuttonclick="onEqButtonEdit" style="width: 100%;" />
                    </div>
                    <div field="FlowID" displayfield="ProcessIdx" width="90" headeralign="center" allowsort="false">
                        生产序号
                        <input property="editor" class="mini-combobox" style="width: 100%;" valuefield="FlowID"
                            textfield="ProcessIdx" />
                    </div>
                    <div field="PlanQty" width="90" headeralign="center" allowsort="false">
                        计划数量
                        <input property="editor" class="mini-spinner" minvalue="0" maxvalue="999999999" style="width: 100%;" />
                    </div>
                    <div field="PlanStartTime" width="120" headeralign="center" dateformat="yyyy-MM-dd HH:mm:ss"
                        allowsort="false">
                        计划开始时间
                        <input property="editor" class="mini-datepicker" format="yyyy-MM-dd H:mm:ss" timeformat="H:mm:ss"
                            showtime="true" style="width: 100%;" />
                    </div>
                    <div field="PlanEndTime" width="120" headeralign="center" dateformat="yyyy-MM-dd HH:mm:ss"
                        allowsort="false">
                        计划结束时间
                        <input property="editor" class="mini-datepicker" format="yyyy-MM-dd H:mm:ss" timeformat="H:mm:ss"
                            showtime="true" style="width: 100%;" />
                    </div>
                </div>
            </div>
        </div>
    </fieldset>
    <div style="text-align: center; padding: 10px;">
        <a class="mini-button" iconcls="icon-save" onclick="onOk" style="width: 60px; margin-right: 20px;">
            保存</a> <a class="mini-button" iconcls="icon-cancel" onclick="onCancel" style="width: 60px;">
                取消</a>
    </div>
    </form>
    <script type="text/javascript">
        mini.parse();
        var grid = mini.get("datagrid1");
        var form = new mini.Form("form1");
        var viewaction;

        function onPrdIdButtonEdit(e) {
            var btnEdit1 = e.sender;
            mini.open({
                url: "../CommForm/SelectProductForm.aspx",
                title: "选择产品",
                width: 650,
                height: 380,
                onload: function () {
                    //var iframe = this.getIFrameEl();
                    //iframe.contentWindow.SetData(null);
                },
                ondestroy: function (action) {
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);    //必须
                        grid.cancelEdit();
                        var row = grid.getSelected();
                        grid.updateRow(row, {
                            PrdID: data.PrdID,
                            PrdName: data.PrdName
                        });
                    }
                }
            });
        }
        function OnCellCommitEdit(e) {
            var grid = e.sender;
            var record = e.record;
            var field = e.field, value = e.value;
            if (field == "PrdID") {
                grid.updateRow(record, { FlowID: "", ProcessIdx: "" });
            }
        }
        function OnCellBeginEdit(e) {
            var grid = e.sender;
            var record = e.record;
            var field = e.field, value = e.value;
            var editor = e.editor;
            if (field == "FlowID") {
                var id = record.PrdID;
                if (id) {
                    var url = "../Service/BasicDataManageHandler.ashx?action=GetFlowListByCombobox&PrdID=" + id
                    editor.setUrl(url);
                } else {
                    e.cancel = true;
                }

            }
        }
        

        function onEqButtonEdit(e) {
            var btnEdit1 = e.sender;
            mini.open({
                url: "../CommForm/SelectEquipmentForm.aspx",
                title: "选择设备",
                width: 650,
                height: 380,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { action: "Select", LineID: mini.getbyName("LineID").getValue() };
                    iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);    //必须
                        grid.cancelEdit();
                        var row = grid.getSelected();
                        grid.updateRow(row,
                        {
                            EqID: data.EqID,
                            EqName: data.EqName
                        });
                    }
                }
            });
        }

        function SaveData() {
            var data = grid.getData();
            var o = form.getData();
            form.validate();
            if (form.isValid() == false) return;
            if (data.length <= 0) {
                mini.alert("生产计划不能为空！请添加...");
            }
            else {
                var PrdIDCount = 0;
                var EqIDCount = 0;
                var PlanStartTimeCount = 0;
                var PlanEndTimeCount = 0;
                var FlowIDCount = 0;
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PrdID == undefined) {
                        PrdIDCount = PrdIDCount + 1;
                    }
                    if (data[i].EqID == undefined) {
                        EqIDCount = EqIDCount + 1;
                    }
                    if (data[i].FlowID == undefined) {
                        FlowIDCount = FlowIDCount + 1;
                    }
                    if (data[i].PlanStartTime == undefined) {
                        PlanStartTimeCount = PlanStartTimeCount + 1;
                    }
                    if (data[i].PlanEndTime == undefined) {
                        PlanEndTimeCount = PlanEndTimeCount + 1;
                    }
                }
                if (PrdIDCount > 0) {
                    mini.alert("列表中产品有为空的，请选择！");
                    return;
                }
                if (EqIDCount > 0) {
                    mini.alert("列表中设备有为空的，请选择！");
                    return;
                }
                if (FlowIDCount > 0) {
                    mini.alert("列表中生产序号有为空的，请填写！");
                    return;
                }
                if (PlanStartTimeCount > 0) {
                    mini.alert("列表中计划开始时间有为空的，请选择！");
                    return;
                }
                if (PlanEndTimeCount > 0) {
                    mini.alert("列表中计划结束时间有为空的，请选择！");
                    return;
                }
                saveData(o, data);
            }
        }

        function saveData(o, data) {
            o.arrList = data;
            var json = mini.encode([o]);
            if (viewaction == "new") {
                $.ajax({
                    url: "../Service/BasicDataManageHandler.ashx?action=AddPrdTask",
                    data: { data: json },
                    type: 'POST',
                    success: function (text) {
                        CloseWindow("save");
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                    }
                });
            }
        }

        ////////////////////
        //标准方法接口定义
        function SetData(data) {
            if (data.action == "new") {
                viewaction = data.action;
                //跨页面传递的数据对象，克隆后才可以安全使用
                data = mini.clone(data);
                mini.getbyName("BizDate").setValue(new Date().format("yyyy-MM-dd"));
            }
        }

        function GetData() {
            var o = form.getData();
            return o;
        }
        function CloseWindow(action) {
            if (action == "close" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                    return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();
        }

        function onOk() {
            SaveData();
        }
        function onCancel(e) {
            CloseWindow("cancel");
        }
        function CloseWindow(action) {
            if (action == "close" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                    return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();
        }
        function addRow() {
            var newRow = { name: "New Row" };
            grid.addRow(newRow, 0);
        }
        function removeRow() {
            var rows = grid.getSelecteds();
            if (rows.length > 0) {
                grid.removeRows(rows, true);
            }
        }
    </script>
</body>
</html>
